<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
    <title>芳小微宣</title>
    <script src="js/jquery.min.js"> </script>
    <script src="js/index.js"></script>
    <!-- 引入 WeUI -->
    <link rel="stylesheet" href="http://res.wx.qq.com/open/libs/weui/0.4.0/weui.min.css">
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="js/uploader/uploader.css">
    <script src="js/uploader/Q.js"></script>
    <script src="js/uploader/Q.Uploader.js"></script>
    <script src="js/uploader/Q.Uploader.UI.js"></script>
    <style>
        .contentin {
            padding-top: 0px;
            margin-left: 0px;
        }
        .content{
            float: none;
        }
        .sp-addfile{
            font-size: 4.8rem;
            color: #adadad;
            margin: 10px 20px;
        }
        .classMc{
            margin-top: 0px;
            width: 178px;
            background: #e0e0e0;
        }
    </style>
</head>
<body>
<div class="weui_cells_title">班级视频上传</div>
<div class="sp-addfile">选择班级</div>
<select class="selSty classMc">
</select>

<!--<div class="selDiv" style="text-align: center">-->
    <!--<span style="font-size: 4.8rem;">选择班级：</span>-->
    <!--<select class="selSty classMc">-->
        <!--<option>请选择</option>-->
    <!--</select>-->
<!--</div>-->
<div class="sp-addfile">上传视频<span style="font-size: 3rem">(请上传一个视频)</span></div>
<div class="content">
    <div class="contentin">
        <div>
            <a id="upload-targetFk" class="x-button" style="_background:#2783ce;_color:white;_text-align:center;_display:inline-block;_height:40px;_line-height:40px;_width:130px;">点击上传视频</a>
        </div>
    </div>
</div>
<div id="upload-viewFk"></div>
<input type="button" class="photo_subCon" value="提交" onclick="subInfo()"/>
<div class="weui_dialog_alert" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd"> <strong class="weui_dialog_title">警告</strong>
        </div>
        <div class="weui_dialog_bd">弹窗内容，告知当前页面信息等</div>
        <div class="weui_dialog_ft">
            <a href="javascript:;" class="weui_btn_dialog primary">确定</a>
        </div>
    </div>
</div>
<script>
    $.weui = {};
    $.weui.alert = function(options ,tag , hrefWhat){
        options = $.extend({title: '提示', text: '提示内容'}, options);
        var $alert = $('.weui_dialog_alert');
        $alert.find('.weui_dialog_title').text(options.title);
        $alert.find('.weui_dialog_bd').text(options.text);
        $alert.on('touchend click', '.weui_btn_dialog', function(){
            if(tag == "succ"){
                if(hrefWhat && hrefWhat == "gg"){
                    window.location.href = "index.html";
                }
            }else if(tag == "err"){
                $alert.hide();
            }
            //阻止事件冒泡
            return false;
        });
        $alert.show();
    };

    var filesid = new Array();
    var removeFilesid = new Array();
    var uploadGroup =[];
    var contentEditor;
    $(function(){

        $.ajax({
            type : "POST",
            url : path + "/api/weixin/getClassBySchool?token=" + window.localStorage.getItem("token"),
            data : {},
            dataType : 'json',
            async : true,
            success : function(msg) {
                if(msg.code == 0){
                    var imghtml = "";
                    for(var i=0;i<msg.classList.length;i++){
                        imghtml += '<option value="'+ msg.classList[i].classId +'">'+ msg.classList[i].className +'</option>';
                    }
                    $(".classMc").append(imghtml);
                    $(".classMc").find("option").eq(0).attr("selected",'selected');
                    $(".classMc").change();
                }else if(msg.code == 501){
                    window.location.href = "login.html";
                }
            }
        });

        uploadFj("upload-targetFk" , "upload-viewFk" ,filesid, removeFilesid ,true);

    });
    //附件上传
    function uploadFj(targetEle , viewEle ,filesid , removeFilesid,isOver){
        var Uploader = Q.Uploader;
        var uploader = new Uploader({
            url: path + "/api/weixin/addClassVideo?token=" + window.localStorage.getItem('token'),
            target: document.getElementById(targetEle),
            view: document.getElementById(viewEle),
            upName: "file",
            multiple: false,
            //每次上传都会发送的参数(POST方式)
            data: { user: "Devin"},
            contentType : false,
            on: {
                //任务移除后触发
                remove: function (task) {
                    var json = task.jsonData;
                    removeFilesid.push({"videoUrl":json.url});
                },
                //上传之前触发
                upload: function (task) {
                    var extension = task.ext;
                    if (!(extension && /^(.mp4|.avi|.3gp)$/.test(extension.toLowerCase()))){
                        $.weui.alert({text: "只支持mp4，3gp和avi格式的视频！"},"err");
                        $("#" + viewEle).find($(".u-item")).eq($("#" + viewEle).find($(".u-item")).length-1).remove();
                        return false;
                    }else if($("#" + viewEle).find($(".u-item")).length >=2){
                        $("#" + viewEle).find($(".u-item")).eq(1).remove();
                        $.weui.alert({text: "只能上传1个视频！"},"err");
                        isOver = false;
                    }
                    $(".photo_subCon").attr("disabled", true);
                },
                //上传完成后触发
                complete: function (task) {
                    $(".photo_subCon").attr("disabled", false);
                    var jsonFj = task.jsonData;
                    if(jsonFj){
                        if (this.index >= this.list.length - 1) {
                            //所有任务上传完成
                            filesid.push({"videoUrl":jsonFj.url});
                            if(isOver == false){
                                removeFilesid.push({"videoUrl":jsonFj.url});
                                isOver = true;
                            }
                        }
                    }
                }
            }
        });
        uploadGroup.push(uploader);
    };
    function subInfo(){
         if(filesid.length <= removeFilesid.length){
            $.weui.alert({text: "请上传视频！"},"err");
        }else{
            $.ajax({
                type : "POST",
                url : path + "/api/weixin/addClassVideoInfo",
                data : {
                    fileList : JSON.stringify(filesid),
                    delList : JSON.stringify(removeFilesid),
                    token : window.localStorage.getItem("token"),
                    classId:$(".classMc").val()
                },
                dataType : 'json',
                success : function(msg) {
                    if(msg.code == 0){
                        $.weui.alert({text: "提交成功！"},"succ" ,"gg");
                    }else if(msg.code == 501){
                        window.location.href = "login.html";
                    }
                }
            });
        }
    }

</script>
</body>
</html>